package com.clsz.service.impl;

import com.alibaba.fastjson.JSON;
import com.clsz.bean.bo.OrdersearchBo;
import com.clsz.bean.bo.SiteBo;
import com.clsz.bean.bo.SiteChooseBo;
import com.clsz.service.OrderService;
import com.clsz.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public boolean addOrder(OrdersearchBo ordersearchBo, String authrization) {
        //1验证订单的冻结信息
        for (SiteBo siteBo : ordersearchBo.getSiteBoList()) {
            SiteChooseBo chooseBo = new SiteChooseBo();
            chooseBo.setWatchTimesId(ordersearchBo.getWatchTimesId());
            chooseBo.setSiteBo(siteBo);
            String key = "Site-Frozen-"+ JSON.toJSONString(chooseBo);
            if ((!redisUtils.hasKey(key)) || (!redisUtils.get(key).toString().equals(authrization))) {
                return false;
            }
        }
        //2发送订单信息到mq的order话题
        kafkaTemplate.send("order", JSON.toJSONString(ordersearchBo), authrization);
        return true;
    }
}
